package cc.yaomm.service.basic.controller.common;

import cc.yaomm.common.basic.base.SuperController;
import cc.yaomm.common.basic.domain.Response;
import cc.yaomm.common.basic.enums.OSSDirectory;
import cc.yaomm.common.basic.file.FileService;
import cc.yaomm.service.basic.controller.common.biz.CommonBiz;
import cc.yaomm.service.basic.controller.common.vo.ImgCaptchaParamVO;
import cc.yaomm.service.basic.controller.common.vo.RoutesResultVO;
import cc.yaomm.service.basic.enums.UploadDirectory;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = {"${apps.biz}/common", "${apps.shop}/common"})
@Api(tags = "公共接口")
@Slf4j
@Validated
public class CommonController extends SuperController {

    @Autowired
    private FileService fileService;

    @Autowired
    private CommonBiz commonBiz;

    @PostMapping("getImgCaptcha")
    @ApiOperation(value = "获取图片验证码", notes = "获取图片验证码")
    public Response<String> getImgCaptcha(@RequestBody @Valid ImgCaptchaParamVO paramVO) {
        return Response.success(commonBiz.getImgCaptcha(paramVO));
    }

    @PostMapping("uploadImage")
    @ApiOperation(value = "上传图片接口", notes = "通用上传图片接口")
    public Response<String> uploadImage(
            @RequestParam("file") @NotNull MultipartFile file,
            @ApiParam(name = "type", value = "文件类型", required = true) @RequestParam(value = "type") @NotBlank final UploadDirectory type) {
        try {
            String filePath = fileService.uploadImage(file.getName(), file.getBytes(), OSSDirectory.getOSSDirectory(type.getValue()), false);
            return Response.success(filePath);
        } catch (IOException e) {
            log.error("上传文件失败", e);
            return Response.fail("上传失败");
        }
    }

    @GetMapping("routes")
    public Response<List<RoutesResultVO>> routes() {
        List<RoutesResultVO> resultVOList = new ArrayList<>();
        resultVOList.add(
                RoutesResultVO
                        .builder()
                        .router("root")
                        .children(Lists.newArrayList(
                                RoutesResultVO
                                        .builder()
                                        .router("parent1")
                                        .children(Lists.newArrayList(RoutesResultVO
                                                .builder()
                                                .router("demo")
                                                .name("demo1")
//                                                .authority("demo")
                                                .build()))
                                        .build(),
                                RoutesResultVO
                                        .builder()
                                        .router("parent2")
                                        .children(Lists.newArrayList(RoutesResultVO
                                                .builder()
                                                .router("demo")
                                                .name("demo2")
                                                .build()))
                                        .build(),
                                RoutesResultVO
                                        .builder()
                                        .router("exception")
                                        .children(Lists.newArrayList(
                                                RoutesResultVO
                                                        .builder()
                                                        .router("exp404")
                                                        .build(),
//                                                RoutesResultVO
//                                                        .builder()
//                                                        .router("exp403")
//                                                        .build(),
                                                RoutesResultVO
                                                        .builder()
                                                        .router("exp500")
                                                        .name("测试500")
                                                        .path("测试501")
                                                        .build()))
                                        .build(),
                                RoutesResultVO
                                        .builder()
                                        .router("demo")
                                        .icon("file-ppt")
                                        .name("验权页面")
//                                        .authority("form")
                                        .build()
                                )
                        )
                        .build());

        return Response.success(resultVOList);
    }

//    @PostMapping("sendCode")
//    @ApiOperation(value = "发送手机验证码", notes = "发送手机验证码")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "string", paramType = "form"),
//            @ApiImplicitParam(name = "type", value = "短信类型<br>" +
//                    "IDENTITY_VERIFIVATION_CODE 身份验证验证码<br>" +
//                    "LOGIN_CONFIRM_CODE 登录确认验证码<br>" +
//                    "LOGIN_ERROR_CODE 登录异常验证码<br>" +
//                    "RIGISTER_VERIFIVATION_CODE 用户注册验证码<br>" +
//                    "PASSWORD_VERIFIVATION_CODE 修改密码验证码<br>" +
//                    "INFOMATION_VERIFIVATION_CODE 信息变更验证码", required = true, dataType = "com.yuncai.common.util.AliSmsUtils.TemplateCode", paramType = "form")})
//    public Notify<Boolean> sendCode(@JsonParam(value = "phone") final String phone, @JsonParam(value = "type") final AliSmsUtils.TemplateCode type) {
//        if (!ValidateUtils.validatePhone(phone)) {
//            return new Notify(Notify.Code.FAILED, "手机格式不正确");
//        }
//
//        if (AliSmsUtils.TemplateCode.RIGISTER_VERIFIVATION_CODE.equals(type)) {
//            if (commonBusiness.registered(phone)) {
//                return new Notify<>(Notify.Code.FAILED, "该手机号已被注册");
//            }
//        }
//
//        String code = StringsUtils.randomCode(6, false, false);
//        redisTemplate.opsForValue().set("Code_" + phone, code, 300, TimeUnit.SECONDS);
//
//        logger.info("验证码：[{}]", code);
//        Map<String, Object> params = Maps.newHashMap();
//        params.put("code", code);
//        if (AliSmsUtils.sendSms(phone, params, type)) {
//
//            return new Notify(true);
//        } else {
//            return new Notify(Notify.Code.FAILED, "发送验证失败，请稍候重试");
//        }
//    }
//
//    @PostMapping("getCode")
//    @ApiOperation(value = "获取手机验证码", notes = "获取手机验证码")
//    @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "string", paramType = "form")
//    public Notify getCode(@JsonParam String phone) {
//        return new Notify(redisTemplate.opsForValue().get("Code_" + phone));
//    }
//
//    @PostMapping("checkCode")
//    @ApiOperation(value = "验证验证码", notes = "验证验证码")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "string", paramType = "form"),
//            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "string", paramType = "form")})
//    public Notify checkCode(@JsonParam String phone, @JsonParam String code) {
//        return new Notify(commonBusiness.checkCode(phone, code));
//    }

}
